package cn.com.se.sharepictrue.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;

import com.loopj.android.http.AsyncHttpClient;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.com.se.sharepictrue.R;
import cn.com.se.sharepictrue.application.Data;
import cn.com.se.sharepictrue.bean.UserInfoBean;
import cn.com.se.sharepictrue.business.CallBack;
import cn.com.se.sharepictrue.business.NetworkResponse;
import cn.com.se.sharepictrue.business.ShareApi;
import cn.com.se.sharepictrue.utils.BusinessUtils;
import cn.com.se.sharepictrue.utils.DialogUtils;
import cn.com.se.sharepictrue.utils.ToastUtils;
import cn.com.se.sharepictrue.utils.ValidateUtils;

/**
 * 注册界面
 * Created by KIDNG on 2015/11/3.
 */
public class RegisterActivity extends Activity  {
    @Bind(R.id.til_register_phone)
    TextInputLayout mTilRegisterPhone;
    @Bind(R.id.til_register_password)
    TextInputLayout mTilRegisterPassword;
    private AsyncHttpClient mClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        mTilRegisterPassword.setHint(getResources().getString(R.string.register_enter_password));
        mTilRegisterPhone.setHint(getResources().getString(R.string.register_enter_phone));
        setTextInputListener();
    }

    private void setTextInputListener() {
        mTilRegisterPassword.getEditText().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTilRegisterPassword.setErrorEnabled(false);
            }
        });
        mTilRegisterPassword.getEditText().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTilRegisterPassword.setErrorEnabled(false);
            }
        });
        mTilRegisterPhone.getEditText().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTilRegisterPhone.setErrorEnabled(false);
            }
        });
    }

    @OnClick(R.id.btn_register_register)
    public void onClick(View v) {
        hideKeyboard();
        String username = mTilRegisterPhone.getEditText().getText().toString();
        String password = mTilRegisterPassword.getEditText().getText().toString();
        if (!ValidateUtils.validatePhone(username)) {
            mTilRegisterPhone.setError(getResources().getString(R.string.phone_error));
        } else if (!ValidateUtils.validatePassword(password)) {
            mTilRegisterPhone.setErrorEnabled(false);
            mTilRegisterPassword.setError(getResources().getString(R.string.password_length_error));
        } else {
            mTilRegisterPhone.setErrorEnabled(false);
            mTilRegisterPassword.setErrorEnabled(false);
            doRegister(username, password);
        }
    }

    private ProgressDialog mProgressDialog;

    private void doRegister(String username, String password) {
        ShareApi.register(this, username, password, new CallBack<NetworkResponse<String>>(this) {
            @Override
            public void onFail() {
                ToastUtils.show(RegisterActivity.this,"网络访问失败...orz");
            }

            @Override
            public void onResponse(NetworkResponse<String> response) {
                if(response.isStatus()){
                    Intent intent = new Intent(RegisterActivity.this, HomeActivity.class);
                    startActivity(intent);
                }else{
                    ToastUtils.show(RegisterActivity.this,response.getError());
                }
            }
        });
    }

//    @Override
//    protected void onStop() {
//        super.onStop();
//        if (mClient != null) {
//            mClient.cancelRequests(this, true);
//        }
//        mClient = null;
//
//    }
//
    private void hideKeyboard() {
        View view = getCurrentFocus();
        if (view != null) {
            ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).
                    hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }
//
//    @Override
//    public void start() {
//        mProgressDialog = DialogUtils.getProgressDialog(RegisterActivity.this, getString(R.string.register_start));
//        mProgressDialog.show();
//    }
//
//    @Override
//    public void success(String msg) {
//
//        ShareApi.userInfo(this, new CallBack<NetworkResponse<UserInfoBean>>() {
//            @Override
//            public void onFail() {
//                mProgressDialog.dismiss();
//            }
//
//            @Override
//            public void onResponse(NetworkResponse<UserInfoBean> response) {
//                mProgressDialog.dismiss();
//                if (response.isStatus()) {
//                    ((Data) (RegisterActivity.this.getApplication())).setUserInfo(response.getData());
//                    Intent intent = new Intent(RegisterActivity.this, HomeActivity.class);
//                    startActivity(intent);
//                }
//            }
//        });
//
//    }
//
//    @Override
//    public void fail(String msg) {
//        mProgressDialog.dismiss();
//        DialogUtils.getMessageDialog(this, msg.isEmpty() ? getString(R.string.internet_fail) : msg).show();
//    }
//
//    @Override
//    public void error(String msg) {
//        mProgressDialog.dismiss();
//        DialogUtils.getMessageDialog(this, getString(R.string.internet_error)).show();
//    }
}
